#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct stu
{
    char name[20];
    int age;
} stu_t;

typedef struct node
{
    stu_t *stus;       // 数据域
    struct node *next; // 指针域
} node_t;


node_t *list_Create()
{
    node_t *head = (node_t *)malloc(sizeof(node_t));
    if (head == NULL)
    {
        printf("malloc faild\n");
        return NULL;
    }
    head->stus = NULL;
    head->next = head;
    return head;
}

int list_tail_insert(node_t *H, stu_t *data)
{
    if (H == NULL || data == NULL)
    {
        printf("error\n");
        return -1;
    }
    node_t *newnode = (node_t *)malloc(sizeof(node_t));
    if (newnode == NULL)
    {
        printf("newnode malloc faild\n");
        return -1;
    }
    newnode->stus = (stu_t *)malloc(sizeof(stu_t));
    if (newnode->stus == NULL)
    {
        printf("malloc faild\n");
        return -1;
    }
    memcpy(newnode->stus, data, sizeof(stu_t));
    newnode->next = NULL;

    // 修改指针
    newnode->next = H;
    node_t *phead = H;
    while (phead->next != H)
    {
        phead = phead->next;
    }
    phead->next = newnode; // 插入
    return 0;
}

void print_data(stu_t *data)
{
    printf("姓名:%s    年龄:%d\n", data->name, data->age);
}

void list_show(node_t *H)
{
    if (H == NULL )
    {
        return;
    }
    if (H->next == H)
    {
        printf("list is empty\n");
        return;
    }

    node_t *phead = H->next;

    while (phead != H)
    {
        print_data(phead->stus);
        phead = phead->next;

    } 
    return;
}



int main(int argc, char const *argv[])
{
    node_t *dlinklist = list_Create();
    stu_t s1 = {"张三", 18};
    stu_t s2 = {"李四", 19};
    stu_t s3 = {"王五", 20};

    list_tail_insert(dlinklist, &s1);
    list_tail_insert(dlinklist, &s2);
    list_tail_insert(dlinklist, &s3);
    list_show(dlinklist);
    return 0;
}
